
package carmadness.view;

import java.awt.*;
import java.util.Iterator;

import carmadness.model.*;

public class GraphicFrontend extends Frame
{
	GameViewport gameViewport;

	public GameViewport getGameViewport()
	{
		return gameViewport;
	}

	Button newGameButton;

	public Button getNewGameButton()
	{
		return newGameButton;
	}

	Button exitButton;

	public Button getExitButton()
	{
		return exitButton;
	}

	Label statusLabel;

	public Label getStatusLabel()
	{
		return statusLabel;
	}

	public GraphicFrontend()
	{
		super( "CarmaDness" );

		// Layout
		GridBagLayout layoutMgr = new GridBagLayout();
		GridBagConstraints layoutConst = new GridBagConstraints();

		setLayout( layoutMgr );
		setBounds( 20, 20, 640, 570 );
		setResizable( true );

		// Játéktér
		gameViewport = new GameViewport();
		gameViewport.setPreferredSize( new Dimension( 500, 500 ) );
		gameViewport.setBackground( Color.DARK_GRAY );

		layoutConst.anchor = GridBagConstraints.NORTHWEST;
		layoutConst.insets = new Insets( 5, 5, 0, 0 );
		layoutMgr.setConstraints( gameViewport, layoutConst );

		add( gameViewport );

		// Menü
		MenuBar menu = new MenuBar();

		layoutConst.anchor = GridBagConstraints.NORTH;
		layoutConst.fill = GridBagConstraints.BOTH;
		layoutConst.gridwidth = GridBagConstraints.REMAINDER;
		layoutConst.insets = new Insets( 5, 5, 0, 5 );
		layoutConst.weightx = 1.0;
		layoutMgr.setConstraints( menu, layoutConst );

		add( menu );

		// Státusz üzenetek
		statusLabel = new Label( "CarmaDness" );
		statusLabel.setBackground( Color.LIGHT_GRAY );
		statusLabel.setAlignment( Label.CENTER );

		layoutConst.insets = new Insets( 5, 5, 5, 5 );
		layoutConst.weighty = 1.0;
		layoutMgr.setConstraints( statusLabel, layoutConst );

		add( statusLabel );

		// Megjelenítés
		setVisible( true );
	}

	/**
	 * A jobboldali menüt leíró Panel leszármazott osztály.
	 * 
	 * @author Ujhelyi
	 */
	private class MenuBar extends Panel
	{
		public MenuBar()
		{
			GridBagLayout layoutMgr = new GridBagLayout();
			GridBagConstraints layoutConst = new GridBagConstraints();
			Dimension buttonDimenstion = new Dimension( 100, 30 );

			setBackground( Color.LIGHT_GRAY );

			setLayout( layoutMgr );
			layoutConst.anchor = GridBagConstraints.NORTH;
			layoutConst.gridwidth = GridBagConstraints.REMAINDER;
			layoutConst.insets = new Insets( 10, 0, 0, 0 );

			// Új játék
			newGameButton = new Button( "Új játék" );
			newGameButton.setPreferredSize( buttonDimenstion );

			layoutMgr.setConstraints( newGameButton, layoutConst );
			add( newGameButton );

			// Kilépés
			exitButton = new Button( "Kilépés" );
			exitButton.setPreferredSize( buttonDimenstion );

			layoutConst.weighty = 1.0;
			layoutMgr.setConstraints( exitButton, layoutConst );
			add( exitButton );
		}
	}

	/**
	 * A játékteret leíró Canvas leszáramzott osztály.
	 * 
	 * @author Ujhelyi
	 */
	public class GameViewport extends Canvas
	{
		Image offscreen;

		/**
		 * Ez a metódus hívódik meg, amikor nem kell teljes újrajzolást
		 * csinálnunk ld.
		 * <link>http://java.sun.com/products/jfc/tsc/articles/painting/</link>
		 */
		public void update( Graphics graphics )
		{
			paint( graphics );
		}

		/**
		 * Ez a metódus hívódik meg teljes újrajzoláskor ld.
		 * <link>http://java.sun.com/products/jfc/tsc/articles/painting/</link>
		 */
		public void paint( Graphics graphics )
		{
			if ( offscreen == null ) offscreen = createImage( 500, 500 );

			GraphicRenderer.setGraphics( (Graphics2D)offscreen.getGraphics() );

			Iterator<RoadSegment> roadSegments = World.getInstance().getRSIterator();

			while ( roadSegments.hasNext() )
			{
				RoadSegment rs = roadSegments.next();
				Renderer.getRenderer( rs ).render( rs );
			}

			graphics.drawImage( offscreen, 0, 0, null );
		}
	}
}
